<!--
Date: 2025-08-13
Copyright (c) 2025 CAX Conversion Project
-->
<template>
  <el-dialog v-model="props.addVisible" :title="modalType === 'edit' ? '修改型材' : '新增型材'" @close="handleVisible('cancel')">
    <el-form size="small" label-width="120px">
      <el-form-item label="型材类型" prop="profileType">
        <el-select @change="typeChange" v-model="formData.type">
          <el-option v-for="item in profileTypes" :label="item.label" :value="item.value" :key="item.label"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-for="item in formItems" :key="item.label" :label="item.label" :prop="item.value">
        <el-input v-model.trim="formData[item.value]" placeholder="请输入" clearable />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button size="small" @click="handleVisible('cancel')">取消</el-button>
        <el-button size="small" type="primary" @click="submitAdd"> 确认 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<!-- 新增型材 -->
<script name="addProfile" setup >
import {
  profileTypes,
  formItemsForCap,
  formItemsForT,
  formItemsForI,
  formItemsForFI,
  formItemsForFz,
  formItemsForFZFF,
  formItemsForFZAFF,
  formItemsForFC,
  formItemsForFCSF,
  formItemsForFL,
  formItemsForFLATI,
  formItemsForFLA,
  formItemsForFGO,
  formItemsForFGM,
  formItemsForFGI,
  formItemsForFGATIW,
  formItemsForFGTI,
  formItemsForFGA
} from '../profileLibrary.js';
import { saveProfile } from '@/api/profileLibrary';

const props = defineProps(['addVisible', 'rowData']);
const emit = defineEmits(['handleEvent']);
const modalType = props?.rowData ? 'edit' : 'add';
const formItems = ref(formItemsForCap);
const formData = ref({
  type: props?.rowData?.type,
  id: props?.rowData?.id,
});

const typeChange = (event) => {
  formData.value = {
    type: event
  };
  switch (event) {
    case 'CAP': {
      formItems.value = formItemsForCap;
      break;
    }
    case 'T': {
      formItems.value = formItemsForT;
      break;
    }
    case 'I': {
      formItems.value = formItemsForI;
      break;
    }
    case 'FI': {
      formItems.value = formItemsForFI;
      break;
    }
    case 'FI': {
      formItems.value = formItemsForFI;
      break;
    }
    case 'FZ': {
      formItems.value = formItemsForFz;
      break;
    }
    case 'FZFF': {
      formItems.value = formItemsForFZFF;
      break;
    }
    case 'FZAFF': {
      formItems.value = formItemsForFZAFF;
      break;
    }
    case 'FC': {
      formItems.value = formItemsForFC;
      break;
    }
    case 'FCSF': {
      formItems.value = formItemsForFCSF;
      break;
    }
    case 'FL': {
      formItems.value = formItemsForFL;
      break;
    }
    case 'FLATI': {
      formItems.value = formItemsForFLATI;
      break;
    }
    case 'FLA': {
      formItems.value = formItemsForFLA;
      break;
    }
    case 'FGO': {
      formItems.value = formItemsForFGO;
      break;
    }
    case 'FGM': {
      formItems.value = formItemsForFGM;
      break;
    }
    case 'FGI': {
      formItems.value = formItemsForFGI;
      break;
    }
    case 'FGATIW': {
      formItems.value = formItemsForFGATIW;
      break;
    }
    case 'FGTI': {
      formItems.value = formItemsForFGTI;
      break;
    }
    case 'FGA': {
      formItems.value = formItemsForFGA;
      break;
    }
  }
};
typeChange(formData.value.type);
// 修改时，填充表单数据
if (modalType === 'edit') {
  formItems.value.forEach((el) => {
    formData.value[el.value] = props.rowData[el.value];
  });
 
}
const handleVisible = (type) => {
  if (type === 'cancel') {
    emit('handleEvent');
  } else {
    emit('handleEvent', { type: 'refresh' });
  }
};
const submitAdd = () => {
  if( modalType === 'edit'){
    formData.value.id = props.rowData.id;
  }
  saveProfile(formData.value).then((res) => {
    if (res) {
      modalType === 'add'
        ? ElMessage({ showClose: true, message: '新增成功。', type: 'success' })
        : ElMessage({ showClose: true, message: '修改成功。', type: 'success' });
      handleVisible('submit');
    }
  });
};
</script>